package com.wh.service.initial;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.mapper.initial.SupplierMapper;
import com.wh.pojo.Supplier;
import com.wh.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SupplierServiceImpl implements SupplierService{

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public PageResult getSupplierList(PageResult pageResult) {
        Page<Supplier> supplierPage = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"name",pageResult.getQuery());
        supplierPage = supplierMapper.selectPage(supplierPage,queryWrapper);
        pageResult.setTotal((long) supplierPage.getRecords().size()).setRows(supplierPage.getRecords());

        return pageResult;
    }

    @Override
    public void addSupplier(Supplier supplier) {
        supplierMapper.insert(supplier);
    }

    @Override
    public void updataSupplier(Supplier supplier) {
        supplierMapper.updateById(supplier);
    }

    @Override
    public void deleteSupplier(String name) {
        supplierMapper.deleteById(name);
    }
}
